
const app = getApp()

const user = {
    nickName:'',
    major:'',
    gender:'',
    school:'',
    faculty:'',
    grade:'',
    schCode:'',
    skill:[],
    prize:'',
    introduction:'',
    resume:[],
    works:[],
    collect:[],
    teamID:[]
}

const me = (params) => {
  const { openid } = params
  const db = wx.cloud.database()
  return new Promise((resolve, reject) => {
    console.log(openid)
    db.collection('user').where({
      _openid: openid
    })
    .field({
      _openid:false,
      _id:false
    })
    .get({
      success: res => {
        if (res.data.length > 0) {
          resolve({data: res.data[0]})
        } else {
          // 创建
          var data = user
          db.collection('user').add({data})
          .then(res => {
            resolve({ data: {...data, ...res} })
          })
        }
      },
      fail: reject
    })
  }) 
}

const saveUserInfo = (params) => {
  console.log(params)
  const db = wx.cloud.database()
  const app = getApp()
  
  return db.collection('user')
    .where({ _openid: app.globalData.openid})
    .update({ data: params })
    
    
}
const getAvatar = async (params)=>{
  const db = wx.cloud.database()
  const openid = params

  return new Promise ((resolve,reject)=>{
   // console.log("nei"+openid)
    db.collection('user').where({
      _openid:openid
    })
    .field({
      _openid:true,
      nickName:true,
      avatarUrl:true
    })
    .get({
      success:res=>{
        console.log(res)
        resolve({data:res.data[0]})
      },
      fali:reject

      
    })
    
  })
 
}




export default {
  me,
  saveUserInfo,
  getAvatar
 
}